package com.xand.mtgox.connector;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xand.bt.api.MarketImageChangeNotify;

public class MtGoxMarketChangesStore {

	private static final Logger marketDataLogger = LoggerFactory.getLogger("mtgoxMarketData");
	
	private static MtGoxMarketChangesStore instance;

	private List<MarketImageChangeNotify> changes;

	private boolean synchronizationDone = false;
	
	public static MtGoxMarketChangesStore getInstance() {
		if(instance == null)
			instance = new MtGoxMarketChangesStore();

		return instance;
	}

	private MtGoxMarketChangesStore(){
		this.changes = new ArrayList<MarketImageChangeNotify>();
	}
	
	public boolean isSynchronizationDone() {
		return synchronizationDone;
	}

	public void setSynchronizationDone(boolean synchronizationDone) {
		this.synchronizationDone = synchronizationDone;
	}

	public void addMarketChangeNotify(MarketImageChangeNotify marketImageChangeNotify, boolean recovery, boolean realtime) {

		String sRecovery = "";
		String sRealtime = "";
		
		if(recovery) {
			sRecovery = "REC[I]";
		}else{
			sRecovery = "REC[0]";
		}
		
		if(realtime) {
			sRealtime = "RT[I]";
		}else{
			sRealtime = "RT[0]";
		}
		
		StringBuilder trace = new StringBuilder("[");
		trace.append(sRecovery);
		trace.append(" ");
		trace.append(sRealtime);
		trace.append("] [");
		trace.append(marketImageChangeNotify.toString());
		trace.append("]");
		
		marketDataLogger.info(trace.toString());
		
		synchronized(this.changes) {
			this.changes.add(marketImageChangeNotify);
		}

	}

	public List<MarketImageChangeNotify> getMarketChanges() {
		List<MarketImageChangeNotify> ret = new ArrayList<MarketImageChangeNotify>();

		if(this.synchronizationDone) {
			synchronized(this.changes) {
	
				for(MarketImageChangeNotify singleChange : this.changes) {
					ret.add(singleChange);
				}
	
				this.changes.clear();
	
			}
		}
		
		return ret;
	}
}
